import api from '@/core/Api'
import urls from '@/router/urls'
import common from '@/common'

class Service {
  async getPage (params) {
    try {
      let formData = new FormData()
      formData.append('file', params.file)
      formData.append('page', params.page)
      formData.append('size', params.size)
      let res = await api.post(urls.getImageSearch(), formData)

      if (res.data.code === 0) {
        let data = res.data.data
        let page = {}
        page.last = !data.has_more
        page.totalElements = data.result_num
        let content = []
        let brief = {}
        data.result.forEach((item) => {
          console.log(item)
          brief = JSON.parse(item.brief)
          brief.id = item.cont_sign
          brief.score = item.score
          brief.contSign = item.cont_sign
          brief.url = common.repleaceOssHost(brief.url)
          content.push(brief)
        })
        page.content = content
        return page
      }
    } catch (e) {
      console.log(e)
    }
    return {
      last: true,
      content: []
    }
  }

  deleteForBatch (formData) {
    formData.append('_method', 'delete')
    return api.post(urls.deleteImageSearchForBatch(), formData)
  }
  delete (id) {
    return api.post(urls.deleteImageSearch(id), {
      _method: 'delete'
    })
  }
}

export default new Service()
